package cn.tedu.logistics.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;


import cn.tedu.logistics.bean.Manager;
import cn.tedu.logistics.service.ManagerService;
import cn.tedu.logistics.service.PurviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tedu.logistics.bean.Purview;


@Controller
@RequestMapping("/systemSet")
public class ManagerController {

	@Autowired
	private PurviewService purviewService;
	@Autowired
	private ManagerService managerService;
	/**
	 * 当点击权限管理处理的业务逻辑
	 */

	@RequestMapping("managerQuery")
	public String checkPurview(HttpServletRequest request) {
		List<Purview> list = purviewService.readPurview();
		request.setAttribute("purviews", list);
		return "systemSet_managerQuery";
	}
	
	@RequestMapping("updatePurview")
	public String updatePurview(Purview p) {
		purviewService.updatePurview(p);
		return "redirect:managerQuery.do";
	}
	
	@RequestMapping("deleteById.do")
	@Transactional
	public String deleteById(Integer id) {
		purviewService.deleteById(id);
		managerService.deleteById(id);
		return "redirect:managerQuery.do";
	}
	@RequestMapping("/user_edit")
	public String userEdit(){
		return "insertManager";
	}
	/**
	 * /systemSet/addManager.do"
	 */
	@RequestMapping("addManager")
	public String addManager(Manager manager){
		String username = manager.getAdmin();
		 String md5password =
				DigestUtils.md5DigestAsHex(manager.getPWD().getBytes());
		 purviewService.addManager(username,md5password);
		 purviewService.addPurviews();
		 return "redirect:managerQuery.do";
	}
}
